﻿
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WENYU_EIO16P;

namespace CLVisionIOCardNC620.IOCardDevice
{
    public class IOCardE16Device : IOCardE16DeviceBase
    {
        private WENYU_EIO16P.EventInterrupt16 eventInterrupt;//中断事件定义
        public byte RiseEnable = 0xff;//DI端口上升沿中断使能公共变量
        public byte FallEnable = 0xff;//DI端口下降沿中断使能公共变量

        public override int nCurCardID { get; set; }
        public override string SerialNumber { get; set; }
        public override string TypeName { get; set; }
        public override bool IsOpen { get; set; }
        public override int IOCardInputBits { get => 8; }
        public override int IOCardOutputBits { get => 8; }
        public override int IOCardBits => 16;
       
        public override bool CloseIODevice(int CardID, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "CloseIODevice_16 successfully!";
            try
            {
                int Result = 0;
                //Result = WENYU_EIO16P.IOCard16.WY_SetInterruptEnable(CardID, 0);
                //if (Result != 0)
                //{
                //    ErrorCode = -1;
                //    ErrorText = $"device:{CardID} WY_SetInterruptEnable Failed!";
                //    return false;
                //}
                Result = WENYU_EIO16P.IOCard16.WY_Close(CardID);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device:{CardID} WY_Close Failed!";
                    return false;
                }
                IsOpen = false;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device:{CardID} CloseIODevice Failed!" + ex.Message;
                return false;
            }
            return true;
        }

        public override bool GetInPutData(int CardID, out long InPutData, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "GetInPutData successfully!";
            try
            {
                int Result = 0;
                byte inputdata = 0;
                Result = WENYU_EIO16P.IOCard16.WY_GetInPutData(CardID, ref inputdata);
                InPutData = inputdata;
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device:{CardID} WY_GetInPutData Failed!";
                    InPutData = 0;
                    return false;
                }
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device:{CardID} WY_GetInPutData Failed!" + ex.Message + ex.StackTrace;
                InPutData = 0;
                return false;
            }
            return true;
        }

        public override bool GetOutPutData(int CardID, out long OutPutData, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "GetOutPutData successfully!";
            try
            {
                int Result = 0;
                byte outputdata = 0;
                Result = WENYU_EIO16P.IOCard16.WY_GetOutPutData(CardID, ref outputdata);
                OutPutData = outputdata;
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device:{CardID} WY_GetOutPutData Failed!";
                    OutPutData = 0;
                    return false;
                }
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device:{CardID} WY_GetOutPutData Failed!" + ex.Message + ex.StackTrace;
                OutPutData = 0;
                return false;
            }
            return true;
        }

        public override bool OpenIODevice(int CardID, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "OpenIODevice16 successfully!";
            try
            {
                nCurCardID = CardID;
                int Result = 0;
                long VersionNumber = 0;
                Result = WENYU_EIO16P.IOCard16.WY_Open(CardID);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device16:{CardID} WY_Open Failed!";
                    return false;
                }
                Result = WENYU_EIO16P.IOCard16.WY_GetCardVersion(CardID, ref VersionNumber);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device16:{CardID} WY_GetCardVersion Failed!";
                    return false;
                }
                IsOpen = true;
                //*************以下内容为中断设置的内容，如果不使用中断就不需要设置**********/
                //定义中断事件函数。此处定义的函数名为：eventInterrupt
                eventInterrupt = new EventInterrupt16(InterruptFun);
                Result = WENYU_EIO16P.IOCard16.WY_SetInterruptFun(CardID, eventInterrupt);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device16:{CardID} WY_SetInterruptFun Failed!";
                    return false;
                }
                Result = WENYU_EIO16P.IOCard16.WY_SetInterruptEnable(CardID, 1);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device16:{CardID} WY_SetInterruptEnable Failed!";
                    return false;
                }
                RiseEnable = 0xff;//初始化输入端口上升沿中断，方使后面操作使用
                FallEnable = 0xff;//初始化输入端口下降沿中断，方使后面操作使用
                Result = WENYU_EIO16P.IOCard16.WY_SetInterruptPortEnable(CardID, RiseEnable, FallEnable);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device16:{CardID} WY_SetInterruptPortEnable Failed!";
                    return false;
                }
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device16:{CardID} OpenIODevice Failed!" + ex.Message + ex.StackTrace;
                return false;
            }
            return true;
        }

        public override bool ReadInPutbit(int CardID, int PortBit, out int Value, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "ReadInPutbit successfully!";
            try
            {
                int Result = 0;
                int value = 0;
                Result = WENYU_EIO16P.IOCard16.WY_ReadInPutbit(CardID, PortBit, ref value);
                Value = value;
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device:{CardID} WY_ReadInPutbit {PortBit} Failed!";
                    Value = 0;
                    return false;
                }
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device:{CardID} WY_ReadInPutbit {PortBit} Failed!" + ex.Message + ex.StackTrace;
                Value = 0;
                return false;
            }
            return true;
        }

        public override bool ReadOutPutBit(int CardID, int PortBit, out int Value, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "ReadOutPutBit successfully!";
            try
            {
                int Result = 0;
                int value = 0;
                Result = WENYU_EIO16P.IOCard16.WY_ReadOutPutBit(CardID, PortBit, ref value);
                Value = value;
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device:{CardID} WY_ReadOutPutBit {PortBit} Failed!";
                    Value = 0;
                    return false;
                }
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device:{CardID} WY_ReadOutPutBit {PortBit} Failed!" + ex.Message + ex.StackTrace;
                Value = 0;
                return false;
            }
            return true;
        }

        public override bool SetOutPutData(int CardID, long OutPutData, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "SetOutPutData successfully!";
            try
            {
                int Result = 0;
                Result = WENYU_EIO16P.IOCard16.WY_SetOutPutData(CardID, (byte)OutPutData);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device:{CardID} WY_SetOutPutData Failed!";
                    return false;
                }
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device:{CardID} WY_SetOutPutData Failed!" + ex.Message + ex.StackTrace;
                return false;
            }
            return true;
        }

        public override bool WriteOutPutBit(int CardID, int PortBit, int Value, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "WriteOutPutBit successfully!";
            try
            {
                int Result = 0;
                Result = WENYU_EIO16P.IOCard16.WY_WriteOutPutBit(CardID, PortBit, Value);
                if (Result != 0)
                {
                    ErrorCode = -1;
                    ErrorText = $"device:{CardID} WY_WriteOutPutBit {PortBit} Failed!";
                    return false;
                }
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"device:{CardID} WY_WriteOutPutBit {PortBit} Failed!" + ex.Message + ex.StackTrace;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 此函数为中断事件函数
        /// </summary>
        public override void InterruptFun()//此函数为中断事件函数
        {
            byte RiseData = 0;
            byte FallData = 0;
            byte InPutData = 0;
            //Thread.Sleep(1);//如何信号跳动比较大的情况下延时再处理；如果信号比较好的情况下不用延时
            int Result = WENYU_EIO16P.IOCard16.WY_GetInterruptData(nCurCardID, ref RiseData, ref FallData);
            if (Result != 0)
            {
                MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
            }
            Result = WENYU_EIO16P.IOCard16.WY_GetInPutData(nCurCardID, ref InPutData);
            if (Result != 0)
            {
                MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
            }

            if (RiseData != 0xffff)
            {
                if ((RiseData & 0x0001) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort0(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((RiseData & 0x0002) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort1(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((RiseData & 0x0004) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort2(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((RiseData & 0x0008) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort3(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((RiseData & 0x0010) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort4(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((RiseData & 0x0020) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort5(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((RiseData & 0x0040) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort6(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((RiseData & 0x0080) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptRisePort7(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
            }
            if (FallData != 0xffff)
            {
                if ((FallData & 0x0001) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort0(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((FallData & 0x0002) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort1(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((FallData & 0x0004) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort2(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((FallData & 0x0008) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort3(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((FallData & 0x0010) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort4(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((FallData & 0x0020) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort5(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((FallData & 0x0040) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort6(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
                if ((FallData & 0x0080) == 0x0000)
                {
                    Result = WENYU_EIO16P.IOCard16.WY_ClrInterruptFallPort7(nCurCardID);
                    if (Result != 0) MessageBox.Show("函数返回值错误!", "提示", MessageBoxButtons.OK);
                }
            }
            WENYU_EIO16P.IOCard16.WY_ResetInterrupt(nCurCardID);
        }

        public override void Release(int CardID, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "";
           
        }

        public override bool GetCardVersion(int CardID, ref long VerNumber, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "";
            if (IOCard16.WY_GetCardVersion(CardID, ref VerNumber) == 0)
            {
                return true;
            }
            return false;
        }

        public override bool SetInterruptEnable(int CardID, int Enable, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "";
         
            int Result = IOCard16.WY_SetInterruptEnable(CardID, Enable);
            if (Result != 0)
            {
                ErrorCode = -1;
                ErrorText = $"device16:{CardID} WY_Open Failed!";
                return false;
            }
            return true;        
        }
    }
}
